package com.hxd.otc.mvp.login;

import android.content.res.AssetManager;
import android.support.annotation.NonNull;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.login.bean.PhoneCodeBean;
import com.hxd.otc.module.my.contract.PhoneCodeContract;
import com.hxd.otc.module.my.model.UserModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.utils.DataUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 作  者 :  胡庆岭
 * 时  间 :  2018/6/5 14:50
 * 描  述 :  ${电话区号P}
 */
public class PhoneCodePresenter extends BasePresenter<PhoneCodeContract.View> implements PhoneCodeContract.Presenter, PhoneCodeModel.Callback {

    private PhoneCodeContract.View mView;
    private final PhoneCodeModel mModel;
    private final UserModel userModel;
    private List<PhoneCodeBean> mData = new ArrayList<>();

    public PhoneCodePresenter(PhoneCodeContract.View view) {
        super(view);
        this.mView = DataUtils.checkNull(view);
        mModel = new PhoneCodeModel(this);
        userModel = UserModel.getInstance();
    }

    @Override
    public void gainPhoneCode(@NonNull AssetManager assets, @NonNull String path) {
        mModel.gainPhoneCode(assets, path);
    }

    @Override
    public void phoneCodeResult(List<PhoneCodeBean> data) {
        mData.clear();
        if (data != null) {
            mData.addAll(data);
        }
        if (mView != null) {
            mView.resultPhoneCode(data);
        }
    }

    @Override
    public List<PhoneCodeBean> getData() {
        return mData;
    }

    @Override
    public void getZoneCode() {
        userModel.getZoneBean(new BaseObserver<List<PhoneCodeBean>>() {
            @Override
            public void onNext(@NonNull String msg, @NonNull List<PhoneCodeBean> zoneBeans) {
//                phoneCodeResult(zoneBeans);
                mData.clear();
                mData.addAll(zoneBeans);
                mView.setZoneCode(zoneBeans);
            }

            @Override
            public void onError(int errType, @NonNull String errMessage) {

                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<List<PhoneCodeBean>>>bindAutoDispose());
    }
}
